
#include "mish_grad_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"

namespace optiling {

constexpr int32_t CORE_NUM = 1;

constexpr int32_t X_INDEX = 1;
constexpr int32_t TANHX_INDEX = 2;
constexpr int32_t BYTE_BLOCK = 32;

constexpr int32_t WITH_TANHX_BUF_COUNT = (2 + 1) * 1 + 1 + 1;
constexpr int32_t WITHOUT_TANHX_BUF_COUNT = (1 + 1) * 1 + 1 + 2;

static ge::graphStatus TilingFunc(gert::TilingContext* context) {
    MishGradTilingData tiling;
    uint32_t totalCoreNum = CORE_NUM;
    uint64_t ubSizePlatForm;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSizePlatForm);

    const gert::StorageShape* xShape = context->GetInputShape(X_INDEX);
    const ge::DataType dataType = context->GetInputDesc(X_INDEX)->GetDataType();
    int32_t dTypeSize = ge::GetSizeByDataType(dataType);
    int32_t perBlockCount = BYTE_BLOCK / dTypeSize;
    int64_t totalDataCount = xShape->GetStorageShape().GetShapeSize();

    context->SetTilingKey(101);
    uint64_t ubMaxProcCount = ubSizePlatForm / WITHOUT_TANHX_BUF_COUNT / BYTE_BLOCK * perBlockCount;

    auto tanhxShape = context->GetInputShape(TANHX_INDEX);
    if (tanhxShape != nullptr) {
        printf("==zf==get into key 201\n");
        context->SetTilingKey(201);
        ubMaxProcCount = ubSizePlatForm / WITH_TANHX_BUF_COUNT / BYTE_BLOCK * perBlockCount;
    }

    tiling.set_ubMaxProcCount(ubMaxProcCount);
    tiling.set_totalDataCount(totalDataCount);
    tiling.set_loopCount(totalDataCount / ubMaxProcCount);
    tiling.set_tailCount(totalDataCount % ubMaxProcCount);

    context->SetBlockDim(totalCoreNum);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

    printf(
        "==zf==Tiling Data, ubSizePlatForm:%lu, ubMaxProcCount:%lu, totalDataCount:%lu, loopCount:%lu, tailCount:%lu\n",
        ubSizePlatForm, tiling.get_ubMaxProcCount(), tiling.get_totalDataCount(), tiling.get_loopCount(),
        tiling.get_tailCount());

    // 以下代码是ascendebug必须的
    size_t* workspaces = context->GetWorkspaceSizes(1);
    workspaces[0] = 16 * 1024 * 1024;

    return ge::GRAPH_SUCCESS;
}
}  // namespace optiling

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context) {
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}  // namespace ge

namespace ops {
class MishGrad : public OpDef {
public:
    explicit MishGrad(const char* name) : OpDef(name) {
        this->Input("grad")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("tanhx")
            .ParamType(OPTIONAL)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore().SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");
    }
};

OP_ADD(MishGrad);
}  // namespace ops
